package com.formiaomiao.demain.handle;

import com.formiaomiao.demain.expection.DemainException;
import com.formiaomiao.demain.model.Result;
import com.formiaomiao.demain.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常捕获
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        if(e instanceof DemainException){
            DemainException girlException = ((DemainException) e);
            return ResultUtil.error(girlException.getCode(),girlException.getMessage());
        }else{
            LOGGER.error("[系统异常]{}",e);
            return ResultUtil.error(-1,"未知错误");
        }
    }
}
